package com.cloudxingkong.dianyinke.NewTeacher.Ui;

import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.cloudxingkong.dianyinke.NewTeacher.Utils.IsMobile;
import com.cloudxingkong.dianyinke.NewTeacher.Utils.LoginJsonUtils;
import com.cloudxingkong.dianyinke.NewTeacher.Utils.MD5Utils;
import com.cloudxingkong.dianyinke.NewTeacher.Utils.TimerUtils;
import com.cloudxingkong.dianyinke.NewTeacher.config.Constans;
import com.cloudxingkong.dianyinke.R;
import com.cloudxingkong.dianyinke.Uri.teacherUri.ThisUri;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.HashMap;

public class Zhuce extends AppCompatActivity {

	private RadioButton rbStudent;
	private RadioButton rbTeacher;
	private EditText etPhoneNo;
	private EditText etPassword;
	private EditText etYanZhengMa;
	private Button btnGetYzm;
	private Button btnZhuCe;
	private String getPhoneNo;
	private String getYZM;
	private String getPassword;
	private RadioGroup radioGroup;
	private int checkedRadioButtonId;
	private int checkedTeacherBtnId;
	private int checkedStudentBtnId;
	private TimerUtils timerUtils;
	private ProgressDialog progressDialog;
	private ProgressDialog progressDialog1;
	private SharedPreferences sharedPreferences;

	private void initToolbar () {
		Toolbar toolbar = (Toolbar) findViewById (R.id.toolbar);
		toolbar.setNavigationIcon (R.drawable.weibo_catalog_back);
		toolbar.setNavigationOnClickListener (new View.OnClickListener () {
			@Override
			public void onClick (View v) {
				finish ();
			}
		});
	}

	@Override
	protected void onCreate (Bundle savedInstanceState) {
		super.onCreate (savedInstanceState);
		setContentView (R.layout.activity_zhuce_backup);
		initToolbar ();
		rbStudent = (RadioButton) findViewById (R.id.zhuce_rb_student);
		rbTeacher = (RadioButton) findViewById (R.id.zhuce_rb_teacher);
		radioGroup = (RadioGroup) findViewById (R.id.zhuce_rg);
		etPhoneNo = (EditText) findViewById (R.id.zhuce_et_phoneno);
		etPassword = (EditText) findViewById (R.id.zhuce_et_password);
		etYanZhengMa = (EditText) findViewById (R.id.zhuce_et_yzm);
		btnGetYzm = (Button) findViewById (R.id.zhuce_btn_getyzm);
		btnZhuCe = (Button) findViewById (R.id.zhuce_btn_zhuce);

		if (!TextUtils.isEmpty (etPhoneNo.getText ())) {
			getPhoneNo = etPhoneNo.getText ().toString ();
		}
		if (!TextUtils.isEmpty (etYanZhengMa.getText ())) {
			getYZM = etYanZhengMa.getText ().toString ();
		}

		timerUtils = new TimerUtils (60 * 1000, 1000, btnGetYzm);

		sharedPreferences = getSharedPreferences ("data_dyk", 0);

	}


	public void back (View view) {
		finish ();
	}

	/**
	 * 获取验证码
	 *
	 * @param view
	 */
	public void getYzm (View view) {

		if (TextUtils.isEmpty (etPhoneNo.getText ())) {
			Toast.makeText (this, "手机号码为空", Toast.LENGTH_SHORT).show ();
			return;
		} else {
			boolean mobileNO = IsMobile.isMobileNO (etPhoneNo.getText ().toString ());
			if (!mobileNO) {
				Toast.makeText (Zhuce.this, "请输入正确的手机号码", Toast.LENGTH_SHORT).show ();
				return;
			}
			progressDialog1 = ProgressDialog.show (this, null, "请稍等...", true, false);
			getPhoneNo = etPhoneNo.getText ().toString ();

			Log.e ("test", "手机号 " + getPhoneNo);
			RequestParams params = new RequestParams (ThisUri.SERVER_YUMING + ThisUri.REQUEST_YZM);
			params.addBodyParameter ("phone", getPhoneNo);
			params.addBodyParameter ("product", "电音课Android");
			x.http ().post (params, new Callback.CommonCallback<String> () {
				@Override
				public void onSuccess (String result) {
					Log.e ("test", "成功请求验证码信息 " + result);
					Toast.makeText (Zhuce.this, "获取验证码成功！", Toast.LENGTH_SHORT).show ();

					timerUtils.start ();
					progressDialog1.dismiss ();
				}

				@Override
				public void onError (Throwable ex, boolean isOnCallback) {
					Log.e ("test", "失败请求注册信息 " + ex.getMessage ());
					Toast.makeText (Zhuce.this, "获取失败", Toast.LENGTH_SHORT).show ();
					progressDialog1.dismiss ();
				}

				@Override
				public void onCancelled (CancelledException cex) {

				}

				@Override
				public void onFinished () {

				}
			});
		}
	}


	/**
	 * @param view
	 */
	public void zhuce (View view) {

		checkedRadioButtonId = radioGroup.getCheckedRadioButtonId ();
		checkedStudentBtnId = rbStudent.getId ();
		checkedTeacherBtnId = rbTeacher.getId ();
		if (TextUtils.isEmpty (etPhoneNo.getText ())) {
			Toast.makeText (this, "手机号码为空", Toast.LENGTH_SHORT).show ();
			return;
		}
		if (TextUtils.isEmpty (etYanZhengMa.getText ())) {
			Toast.makeText (this, "验证码为空", Toast.LENGTH_SHORT).show ();
			return;
		}
		if (TextUtils.isEmpty (etPassword.getText ())) {
			Toast.makeText (this, "密码为空", Toast.LENGTH_SHORT).show ();
			return;
		}
		if (!rbStudent.isChecked () && !rbTeacher.isChecked ()) {
			Toast.makeText (this, "您还没有选择角色哟~", Toast.LENGTH_SHORT).show ();
			return;
		}

		progressDialog = ProgressDialog.show (this, "提示", "正在注册....", true, false);
		getPhoneNo = etPhoneNo.getText ().toString ();
		getYZM = etYanZhengMa.getText ().toString ();
		getPassword = etPassword.getText ().toString ();
		final String md5 = MD5Utils.getMD5 (getPassword);

		RequestParams params = new RequestParams (ThisUri.SERVER_YUMING + ThisUri.USER_REGISTE);
		params.addBodyParameter ("phone", getPhoneNo);
		params.addBodyParameter ("password", md5);
		params.addBodyParameter ("smscode", getYZM);
		if (checkedRadioButtonId == checkedStudentBtnId) {
			params.addBodyParameter ("memtype", "student");
		} else if (checkedRadioButtonId == checkedTeacherBtnId) {
			params.addBodyParameter ("memtype", "teacher");
		}
		x.http ().post (params, new Callback.CommonCallback<String> () {
			@Override
			public void onSuccess (String result) {
				Message message = handler.obtainMessage ();
				message.what = 1;
				message.obj = result;
				handler.sendMessage (message);
			}

			@Override
			public void onError (Throwable ex, boolean isOnCallback) {
				progressDialog.dismiss ();
				Log.e ("test", "失败请求注册信息 " + ex.getMessage ());
				Toast.makeText (Zhuce.this, "注册失败！", Toast.LENGTH_SHORT).show ();
			}

			@Override
			public void onCancelled (CancelledException cex) {

			}

			@Override
			public void onFinished () {

			}
		});


	}


	private Handler handler = new Handler () {
		@Override
		public void handleMessage (Message msg) {
			super.handleMessage (msg);
			if (msg.what == 1) {
				String result = (String) msg.obj;
				Log.e ("test", "成功请求注册信息 " + result);
				HashMap<String, Object> fromLogin = LoginJsonUtils.getJsonFromLogin (result);
				Log.e ("test", fromLogin.get ("success") + "---" + result);
				boolean b = (boolean) (fromLogin.get ("success"));
				if (b == true) {
					Toast.makeText (Zhuce.this, "注册成功！", Toast.LENGTH_SHORT).show ();

					SharedPreferences.Editor editor = sharedPreferences.edit ();
					editor.putString ("userPhone", getPhoneNo);
					editor.putString ("userPwd", getPassword);
					editor.commit ();

					progressDialog.dismiss ();
					setResult (Constans.LOGIN_TO_REG_REP);
					finish ();
				} else {
					Toast.makeText (Zhuce.this, "注册失败,请检查用户信息！", Toast.LENGTH_SHORT).show ();
					progressDialog.dismiss ();
				}
			}
		}
	};

}
